const Controller = require('egg').Controller;
/**
 * @controller CategoryController 章节相关接口
 */
class CategoryController extends Controller {
  async index() {
    /**
     *  @description 查询书籍的所有章节
     *  @router get /api/category/:bid
     *  @request query string bid 书籍id
     *  @example /api/category/:bid
     */
    const {
      ctx,
    } = this;
    const bid = ctx.params.bid;
    const result = await ctx.service.category.getAllCategories(bid);
    ctx.status = 200;
    ctx.body = result;
  }
  async show() {

    /**
     *  @description 查询某个章节
     *  @router get /api/category/:bid/:cid
     *  @request query string bid 书籍id
     *  @request query string cid 章节id
     *  @example /api/category/:bid/:cid
     */
    const {
      ctx,
    } = this;
    const bid = ctx.params.bid;
    const cid = ctx.params.cid;
    if (bid) {
      if (cid) {
        const result = await ctx.service.category.getAllCategoryById(bid, cid);
        ctx.status = 200;
        ctx.body = result;
      } else {
        const result = await ctx.service.category.getAllCategories(bid);
        ctx.status = 200;
        ctx.body = result;
      }
    } else {
      ctx.status = 400;
      ctx.body = '书籍id或章节id不存在';
    }
  }
  async create() {
    /**
     *  @description 插入章节
     *  @router post /api/addCategory
     *  @request data addCategory
     */
    const rule = {
      bid: {
        type: 'string',
        required: true,
        message: '书籍id不能为空',
      },
      title: {
        type: 'string',
        required: true,
        message: '章节标题不能为空',
      },
      content: {
        type: 'string',
        required: true,
        message: '章节内容不能为空',
      },
    };
    const {
      ctx,
    } = this;
    const data = ctx.request.body;
    await ctx.validate(rule, data);
    const result = await ctx.service.category.addCategory(data);
    ctx.status = 200;
    ctx.body = result;
  }
  async update() {
    /**
     *  @description 更新章节
     *  @router post /api/updateCategory
     *  @request data addCategory
     */
    const rule = {
      id: {
        type: 'string',
        required: true,
        message: '章节id不能为空',
      },
      title: {
        type: 'string',
        required: true,
        message: '章节标题不能为空',
      },
      content: {
        type: 'string',
        required: true,
        message: '章节内容不能为空',
      },
    };
    const {
      ctx,
    } = this;
    const data = ctx.request.body;
    await ctx.validate(rule, data);
    const result = await ctx.service.category.updateCategory(data);
    ctx.status = 200;
    ctx.body = result;
  }
  async destory() {
    /**
     *  @description 删除章节
     *  @router post /api/deleteCategory/
     */
    const rule = {
      id: {
        type: 'string',
        required: true,
        message: '章节id不能为空',
      },
    };
    const {
      ctx,
    } = this;
    const data = ctx.request.body;
    await ctx.validate(rule, data);
    const result = await ctx.service.category.deleteCategory(data.id);
    ctx.status = 200;
    ctx.body = result;
  }

  async addCategoryPvById() {
    /**
     * @description 统计书籍被点击的次数
     * @router GET /api/addCategoryPvById
     * @request query string bid 书籍id
     * @request query string cid 章节id
     */
    const { ctx } = this;
    const bid = ctx.query.bid;
    const cid = ctx.query.cid;
    const result1 = await ctx.service.book.addBookPvById(bid);
    const result2 = await ctx.service.category.addCategoryPvById(cid);
    ctx.status = 200;
    // 无论统计结果成功还是失败，都不把结果暴露给用户
    ctx.body = {
      code: 0,
      data: {
        bookResult: result1,
        categoryResult: result2,
      },

    };
  }
  async getAllCategoryTitle() {
    /**
     *  @description 获取所有章节的标题
     *  @router get /api/getAllCategoryTitle/?bid=
     *  @request query string bid 书籍id
     *  @example /api/getAllCategoryTitle/?bid=1
     */
    const {
      ctx,
    } = this;
    const bid = ctx.query.bid;
    const result = await ctx.service.category.getAllCategoryTitle(bid);
    ctx.status = 200;
    ctx.body = result;
  }


}

module.exports = CategoryController;
